package com.foreverwin.mes.meapi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.meapi.mapper.BomMapper;
import com.foreverwin.mes.meapi.model.Bom;
import com.foreverwin.mes.meapi.model.BomComponent;
import com.foreverwin.mes.meapi.service.BomComponentService;
import com.foreverwin.mes.meapi.service.BomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Syngna
 * @since 2020-02-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BomServiceImpl extends ServiceImpl<BomMapper, Bom> implements BomService {


    @Autowired
    private BomMapper bomMapper;

    @Autowired
    private BomComponentService bomComponentService;

    @Override
    public Page<Bom> selectPage(FrontPage<Bom> frontPage, Bom bom) {
        EntityWrapper<Bom> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(bom);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<Bom> selectList(Bom bom) {
        EntityWrapper<Bom> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(bom);
        return super.selectList(queryWrapper);
    }

    @Override
    public Bom getBomInfo(String site, String bomBo) {
        Bom bom = null;
        if(bomBo.endsWith("#")) {
            String[] temp = bomBo.split(",");
            String bomName = temp[1];
            String type = temp[2];
            List<Bom> bomList = this.selectList(new EntityWrapper<Bom>()
                    .eq(Bom.SITE, site)
                    .eq(Bom.BOM, bomName)
                    .eq(Bom.BOM_TYPE, type)
                    .eq(Bom.CURRENT_REVISION, "true"));
            bom = bomList != null && bomList.size() > 0 ? bomList.get(0) : null;
        } else {
            bom = this.selectById(bomBo);
        }
        if(bom != null) {
            List<BomComponent> bomComponentList = bomComponentService.listByBomBo(bom.getHandle());
            bom.setBomComponentList(bomComponentList);

        }
        return bom;
    }
}